package com.gdkf.dormitory.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdkf.dormitory.entity.Admin;
import com.gdkf.dormitory.entity.MyPage;
import com.gdkf.dormitory.entity.Zhu;
import com.gdkf.dormitory.service.ZhuService;
import com.gdkf.dormitory.tools.MyPageTool;
import com.gdkf.dormitory.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcy
 * @since 2022-09-09
 */
@RestController
public class ZhuController {
    @Autowired
    private ZhuService zhuService;

    @PostMapping("/zhu/getAllZhu")
    public Msg getAllZhu(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);

        Page<Zhu> page = new Page<>(myPage.getCurrentPage(),myPage.getSizePage());
        zhuService.page(page, null);
        List<Zhu> records = page.getRecords();
        return  records.isEmpty()?Msg.fail():Msg.success()
                .add("zhuList",page.getRecords())
                .add("sizePage",page.getSize())
                .add("currentPage",page.getCurrent())
                .add("pageTotal",page.getTotal());
    }

    @PostMapping("/zhu/getOneZhu")
    public Msg getOneZhu(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToZhu(myPage);
            System.out.println("======getOneZhu========>"+myPage2);
            Page<Zhu> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Zhu> wrapper = new QueryWrapper<>();
            wrapper.eq("zhu_id",((Zhu)myPage2.getObject()).getZhuId());
            wrapper.eq("stu_id",((Zhu)myPage2.getObject()).getStuId());
            Page<Zhu> page1 = zhuService.page(page, wrapper);
            List<Zhu> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("zhuList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();
    }

    @PostMapping("/zhu/addZhu")
    public Msg addZhu(@RequestBody Zhu zhu){
        return zhuService.save(zhu)?Msg.success():Msg.fail();
    }

    @PostMapping("/zhu/updateZhu")
    public Msg uodateZhu(@RequestBody Zhu zhu){
        return zhuService.updateById(zhu)?Msg.success():Msg.fail();
    }

    @PostMapping("/zhu/deleteZhu")
    public Msg deleteZhu(@RequestBody Zhu zhu){
        return zhuService.removeById(zhu.getZhuId())?Msg.success():Msg.fail();
    }

    @PostMapping ("/zhu/getDetailInformation")
    public Msg getDetailInformation(@RequestBody Zhu zhu){
//        System.out.println(admin);
        HashMap<String, Object> map = new HashMap<>();
        map.put("stuId",zhu.getStuId());
        Zhu zhu1 = zhuService.selectZhuStudent(map);
        ArrayList<Zhu> zhus = new ArrayList<>();
        zhus.add(zhu1);
        return zhus.isEmpty()?Msg.fail():Msg.success().add("zhuList",zhus);
    }
}

